/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gcalscraper.wapo.date;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author dave
 */
public class Occurrence {
  
  private boolean recurrent;
  private Date start;
  private Date end;
  private String recurSpec;
  
  public Occurrence(Date s, Date e) {
    start = s;
    end = e;
  }
  
  public Occurrence(String r) {
    recurrent = true;
    recurSpec = r;
  }
  
  public static Occurrence createFrom(Calendar base, Date parsed) {
    Calendar c = Calendar.getInstance();
    c.setTime(parsed);
    
    c.set(Calendar.YEAR, base.get(Calendar.YEAR));
    
    Date s = c.getTime();
    
    c.add(Calendar.HOUR_OF_DAY, 1);
    
    Date e = c.getTime();
    
    return new Occurrence(s, e);
  }
  
  public static Occurrence createAllDayFrom(Calendar base, Date parsed) {
    Calendar c = Calendar.getInstance();
    c.setTime(parsed);
    
    c.set(Calendar.YEAR, base.get(Calendar.YEAR));
    c.set(Calendar.HOUR_OF_DAY, 8);
    Date s = c.getTime();
    
    c.set(Calendar.HOUR_OF_DAY, 23);
    
    Date e = c.getTime();
    
    return new Occurrence(s, e);
  }
  
  public static Occurrence createFrom(Calendar base, Date s, Date e) {
    Calendar c = Calendar.getInstance();
    c.setTime(s);
    
    c.set(Calendar.YEAR, base.get(Calendar.YEAR));
    
    Date s2 = c.getTime();
    
    c.setTime(e);
    c.set(Calendar.YEAR, base.get(Calendar.YEAR));
    
    Date e2 = c.getTime();
    
    return new Occurrence(s2, e2);
  }
  
  public boolean isRecurrent() {
    return recurrent;
  }
  
  public Date getStart() {
    return start;
  }
  
  public Date getEnd() {
    return end;
  }
  
  public String recurrenceSpecification() {
    return recurSpec;
  }

  @Override
  public String toString() {
    if (isRecurrent())
      return recurrenceSpecification();
    else
      return getStart() + " to " + getEnd();
  }  
}
